/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: orbis_mnc_agg.do
Date: October 2022

Description: orbis_aggregates

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"
*--------------------------------------------------------


etime, start 
use "${data}/firm_allyears_sales.dta", clear
keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 

gen iso2=substr( id_bvd ,1,2)
gen hs2=substr( guo_bvd,1,2)
tab sector1
drop if hq==isocode
drop if hs2==iso2

drop if hq=="WW" | hq=="ZZ" | hq=="YY"
drop if hq=="KY" | hq=="VG" |  hq=="LU"  |  hq=="CH"
tab con_code
drop if sector1=="Non-Market Economy"

*Inspecting what is happening  
keep year isocode sector sector1 sales emp va exports con_code
collapse (sum) sales emp va, by(isocode year con_code sector sector1)
collapse (sum) sales emp va, by(isocode year con_code)

keep if (year==2016 & isocode!="MX") | ( year==2014 & isocode=="MX")
replace year=2016
sort isocode con_code

by isocode, sort: egen tot=total(sales)
gen share=sales/tot
table isocode con_, c(mean share)
tab con_code
drop if con_code=="LF" & isocode!="JP"
drop if con_code=="U2" & isocode!="MX"
collapse (sum) sales emp va, by(isocode year)

save "${data}/orbis_mnc_agg.dta", replace

